//
//  RequestConfig.m
//  TaduFramework
//
//  Created by 王越 on 14-4-16.
//  Copyright (c) 2014年 惠每. All rights reserved.
//

#import "RequestConfig.h"
#import "iTools.h"
@implementation RequestConfig

/**
 *  获取header
 *
 *  @return <#return value description#>
 */
+ (NSMutableDictionary *)getAllHeaders
{
    NSMutableDictionary * headers = [NSMutableDictionary dictionary];
    [headers setObject:[self getXClient] forKey:@"X-Client"];
    [headers setObject:[self getCOOKIE] forKey:@"COOKIE"];
    [headers setObject:[self UserAgent] forKey:@"User-Agent"];
    [headers setObject:[self ContentType] forKey:@"Content-Type"];
    return headers;
}

/**
 *  获取COOKIE
 *
 *  @return return value description
 */
+ (NSString *)getCOOKIE
{
    @try {
        NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        if([sharedHTTPCookieStorage cookies].count > 0){
            NSArray *cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:[iTools ServerHost]]];
            if(cookies != nil && cookies.count > 0){
                for(int i=0;i<cookies.count;i++){
                    NSHTTPCookie * cookie = cookies[i];//[cookies lastObject];
                    NSString *name = [cookie name];
                    if([name isEqualToString:@"sessionid"]){
                        NSString *sessionid = [cookie value];
                        if(sessionid != nil && sessionid.length > 8){
                            NSLog(@"sessionid is:%@",sessionid);
                            [iTools saveCookieValue:cookie];//保存cookie
                            NSString *COOKIE = [NSString stringWithFormat:@"sessionid=%@",sessionid];
                            return COOKIE;
                        }
                    }
             
                }
            }
        }
    } @catch (NSException *exception) {
        [iTools logException:exception];
    }
    return @"";
}

/**
 * 获取X-Client数据
 */
+ (NSString *)getXClient
{
    NSString *xClient = [NSString stringWithFormat:@"version=%@;accessPoint=0;imei=%@;imsi=ios;mac=;type=%@;sv=%f;screenSize=%@", [iTools getVersion],[iTools getUDID],[iTools getPhoneType],[iTools getSystemVersion],[iTools getScreenSize]];
    
    return xClient;
}

+ (NSString *)ContentType
{
    return @"application/x-www-form-urlencoded";
}

+ (NSString *)UserAgent
{
    return @"Opera/10.61 (J2ME/MIDP; Opera Mini/5.1.21219/19.999; zh-CN; rv:1.9.3a5) WebKit/534.5 Presto/2.6.30";
}

@end
